﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridInGrid
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var cats = GetCategories();
                   LoadCats(cats);
            }
        }

        private void LoadCats(IEnumerable<ProductCategory> cats)
        {
            this.GridView1.DataSource = cats;
            this.GridView1.DataBind();
        }

        private IEnumerable<ProductCategory> GetCategories()
        {
            return new Data().ProductCategories;
            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var  data = e.Row.DataItem as ProductCategory;
                if (data != null)
                {
                    var prods = LoadProds(data.Name);

                    // ui feedback
                    var gridview = e.Row.FindControl("GridView2") as GridView;
                    if (gridview != null)
                    {
                        gridview.DataSource = prods;
                        gridview.DataBind();
                    }

                }
            }
        }

        private IEnumerable<Product> LoadProds(string p)
        {
            return new Data().Products(p); ;
        }
    }

    public class Data
    {
        private static ProductCategory pc = new  ProductCategory() { Name = "PC" , description = "personal computer" };
        private static ProductCategory tab = new  ProductCategory { Name = "TAB" , description = "Tablet" };
        private static ProductCategory sp = new  ProductCategory { Name = "SP" , description = "Smartphone" };
        private static ProductCategory gsm = new  ProductCategory { Name = "GSM" , description = "Celluar phone" };

        private static List<ProductCategory> cats = new List<ProductCategory> { pc,   tab,   sp ,  gsm};


        private static List<Product> prods = new List<Product> { new  Product { ProductCategory = pc , description = "personal computer 1" ,   Name= "PC1"  },
                new  Product { ProductCategory = tab , description = "Tablet 1" , Name= "TAB1" },
                new  Product { ProductCategory = sp , description = "Smartphone 1 " , Name= "SP1" },
                new  Product { ProductCategory = gsm , description = "Celluar phone 1" , Name= "GSM1" },
            };


        public IEnumerable<ProductCategory> ProductCategories
        {
            get
            {
                return cats;
            }
        }

        public IEnumerable<Product> Products(string ProductCategoryName)
        {

            return prods.Where(c => c.ProductCategory.Name == ProductCategoryName);
           
        }
    }
}